package common;
import java.awt.*;
import java.awt.geom.*;

import math.geom2d.conic.CircleArc2D;

/**
   A class that supplies convenience implementations for 
   a number of methods in the Edge interface type.
*/
public abstract class AbstractEdge implements Edge
{  
   public Object clone()
   {
      try
      {
         return super.clone();
      }
      catch (CloneNotSupportedException exception)
      {
         return null;
      }
   }

   public void connect(Node s, Node e)
   {  
      start = s;
      end = e;
   }

   public Node getStart()
   {
      return start;
   }

   public Node getEnd()
   {
      return end;
   }

   public Rectangle2D getBounds(Graphics2D g2)
   {
      Line2D conn = getConnectionLine();      
      Rectangle2D r = new Rectangle2D.Double();
      r.setFrameFromDiagonal(conn.getX1(), conn.getY1(),
         conn.getX2(), conn.getY2());
      return r;
   }

   public Line2D getConnectionLine()
   {
      Rectangle2D startBounds = start.getBounds();
      Rectangle2D endBounds = end.getBounds();
      Point2D startCenter = new Point2D.Double(
         startBounds.getCenterX(), startBounds.getCenterY());
      Point2D endCenter = new Point2D.Double(
         endBounds.getCenterX(), endBounds.getCenterY());
      return new Line2D.Double(
         start.getConnectionPoint(endCenter),
         end.getConnectionPoint(startCenter));
   }
   
   public Arc2D getConnectionArc(double radius, boolean side)
   {
		Point2D p1 = new Point2D.Double(start.getBounds().getCenterX(),
				start.getBounds().getCenterY());
		Point2D p2 = new Point2D.Double(end.getBounds().getCenterX(),
				end.getBounds().getCenterY());
	    
	    Point2D mid = new Point2D.Double((p1.getX() + p2.getX())/2, (p1.getY() + p2.getY())/2);
	    double midDist = p1.distance(mid);
		
	    double alpha = Math.acos(midDist/radius);
	    double h = Math.sin(alpha) * radius;
	    
	    double beta = Math.atan2(p2.getY()-p1.getY(), p2.getX()-p1.getX()) - Math.PI/2;
	    double dx = Math.cos(beta) * h;
	    double dy = Math.sin(beta) * h;
	    
	    Point2D center = side?new Point2D.Double(mid.getX() + dx, mid.getY() + dy):new Point2D.Double(mid.getX() - dx, mid.getY() - dy);
	    Rectangle2D bounds = new Rectangle2D.Double(center.getX() - radius, center.getY() - radius, radius*2, radius*2);
	    
	    Point2D connP1 = getStart().getArcConnectionPoint(p2, radius, side);
	    Point2D connP2 = getEnd().getArcConnectionPoint(p1, radius, !side);
	    
	    Double startAngle = - Math.atan2(connP1.getY() - center.getY(), connP1.getX() - center.getX()) * 180 / Math.PI;
	    Double angleExtent = (- Math.atan2(connP2.getY() - center.getY(), connP2.getX() - center.getX())* 180 / Math.PI) - startAngle;
	    if (angleExtent < 0  && side) angleExtent += 360;
	    if (angleExtent > 0  && !side) angleExtent -= 360;
	    
	    Arc2D arc = new Arc2D.Double(bounds, startAngle, angleExtent, Arc2D.OPEN);
	    return arc;
   }

   protected Node start;
   protected Node end;
}
